.PHONY: clean

OBJECTS   := cfd-util.o   \
			      cfd-ptrace.o \
						 cfd-filter.o \
						 cfd-mmap.o

FILTER    := cfd-proc.o   \
			       cfd-elf.o    \
						 cfd-syms.o   \
						 cfd-mmap.o

FILTER_SO := cfd-syms.so

TARGET    := cfd-util
LIBS      := $(FILTER_SO)

CFLAGS    += -D_GNU_SOURCE -ggdb

OPS       :=

ifdef x86_64
OPS   += ldload.64.op change-stdout.64.op
endif

ifdef x86
OPS   += ldload.32.op change-stdout.32.op
endif


all: $(TARGET) $(LIBS) \
			ldload.so  \
			$(OPS)

$(TARGET): $(OBJECTS)
		$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) -ldl \
					-Wl,-rpath,$$ORIGIN

$(FILTER_SO): $(FILTER)
		$(CC) -shared -o $@ $^ $(CFLAGS) $(LDFLAGS) \
					-Wl,-soname,$(FILTER_SO),--no-undefined

ldload.so : ldload.c
		$(CC) -shared -fPIC -o $@ $^ -Wl,--no-undefined

%.o : %.c
		$(CC) -c -o $@ $< $(CFLAGS) -std=gnu99 -fPIC -Wall

%.op : %.S
		$(AS) -o $@.elf $^
		objcopy -O binary $@.elf $@
		objdump -D $@.elf > $@.list
		rm -f $@.elf

clean:
		rm -f $(OBJECTS) $(TARGET) $(FILTER)
